package org.aileen.springmvc.Controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.aileen.springmvc.model.UserInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")//通常情况下,类上都要加上RequestMapping,这样子就不会和别人的类路径上的方法重复了
@RestController
public class RequestController {

    @RequestMapping("/param1")
    public String param1(String userName){
        System.out.println("接收到参数 : " + userName);
        return "接收到参数: " + userName;
    }

    @RequestMapping("/param2")
    public String param2(Integer age){
        System.out.println("接收到参数 : " + age);
        return "2接收到参数: " + age;
    }

    @RequestMapping("/param3")
    public String param3(int age){
        System.out.println("接收到参数 : " + age);
        return "3接收到参数: " + age;
    }

    //旧接口
    @Deprecated //废弃调旧接口的注解
    @RequestMapping("/param4")
    public String param4(String userName , String password){
        return "接收到参数 : " + userName + ", password : " + password;

    }

    //如果要增加新的业务功能,新添加一个接口
    @RequestMapping("/param6")
    public String param6(String userName , String password,Integer age){
        return "接收到参数 : " + userName + ", password : " + password + ", age : " + age;

    }


    //传递处对象 -> 将所有参数封装成一个值
    @RequestMapping("/param5")
    public String param5(UserInfo userInfo){
        System.out.println(userInfo.getUserName());
        return "接收到参数 : " + userInfo;

    }

    //修改参数名称
    @RequestMapping("/param7")//通过RequestParam注解将前端参数userName与后端参数name进行绑定
    public String param7(@RequestParam(value = "userName" , required = false) String name , String password){
        return "接收到参数 : " + name+ ", password : " + password;

    }


    //传递数组
    @RequestMapping("/param8")
    public String param8(String[] arrParams){
        return "接收到参数arrParams  : " + Arrays.toString(arrParams) ;

    }

    //传递集合
    @RequestMapping("/param9")
    public String param9(@RequestParam("listParam") List<String> listParam){
        return "接收到参数arrParams  : " +  listParam;
    }

    //传递Json数据
    @RequestMapping("/param10")
    public String param10(@RequestBody UserInfo userInfo){
        return "接收到参数userInfo  : " + userInfo  ;
    }

    //获取URL参数
    @RequestMapping("/param11/{articleID}")//因为文章参数就在URL里面了,所以路径需要使用{}拼接上URL,
    public String param11(@PathVariable("articleID") Integer articleID){
        return "接收到参数articleID : " + articleID ;
    }

    //获取URL参数
    @RequestMapping("/param11/{id}/{name}")//因为文章参数就在URL里面了,所以路径需要使用{}拼接上URL,
    public String param12(@PathVariable("id") Integer id , @PathVariable("name") String name){
        return "接收到参数id : " + id + " , name : "+ name ;
    }

    @RequestMapping("/param13")//因为文章参数就在URL里面了,所以路径需要使用{}拼接上URL,
    //上传文件
    public String param13(@RequestPart("file666") MultipartFile file) throws IOException {
//        System.out.println(file.getContentType());
//        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename().length());

        file.transferTo(new File("D:/OneDrive/Desktop/" + file.getName()));
        return "接收到文件 : " + file.getName() ;
    }


    //-------------------------------------------------------------------
    //获取Cookie

    //方式1:
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request ){
//        String userName = request.getParameter("userName");
//        String age = request.getParameter("age");
//        Integer age1 = Integer.parseInt(age);//将age强转成Integer雷星
        //创见Cookie对象
        Cookie[] cookies = request.getCookies();
        System.out.println("获取Cookie信息 : ");
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if("bite".equals(cookie.getName()))
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
        return "获取Cookie成功";
    }

    //方式2:使用注解
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bite") String bite ){
        System.out.println("从Cookie 中获取 bite: " + bite);
        return "获取Cookie成功";
    }

    //Session的创建
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //参数为true 如果session不存在,则创建一个session;如果存在,则直接返回
        HttpSession session = request.getSession(true);
        //设置设置session
        session.setAttribute("name","Aileen");
        session.setAttribute("id","1");
        return "设置session成功";

    }

    //Session的获取
    //方式一:通过HttpServletRequest对象
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        //参数为true 如果session不存在,则创建一个session;如果存在,则直接返回
        HttpSession session = request.getSession(false);
        if(session != null){
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("id"));
        }
        return "获取session成功";
    }

    //方式二:方法一的简化版
    //将request.getSession();的引用HttpSession session直接传入到方法里面,因为()里面默认会是true,不会出现null的错误
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        //session = request.getSession();
        if(session != null){
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("id"));
        }
        return "方式2,获取session成功";
    }

//    //方式三:使用注解
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name" , required = false)String name){
        System.out.println("name : "+ name );
        return "方式3获取session成功";

    }

    //获取Header
    //方式1:传统法
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String UserAgent = request.getHeader("User-Agent");
        return "Header中的 userAgent是:"+UserAgent;
    }

    //方式2:使用注解
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String agent){
        return " User-Agent : "+ agent;
    }

}










































